package lianxi7;

import java.util.stream.LongStream;
import java.util.stream.*;

/**
 * 
 * @author dell
 */
public class ParallelStreams {

	public static long iterativeSum(long n){
		long result =0;
		for(long i=0;i<=n ;i++){
			result+=i;
		}
		return result ;
	}
	
	//顺序执行
	public static long sequentialSum(long n){
		return Stream.iterate(1L, i->i+1).limit(n).reduce(Long::sum).get();
	}
	
	//并行计算
	public static long parallelSum(long n ){
		return Stream.iterate(1L, i->i+1).limit(n).parallel().reduce(Long::sum).get();
	}
	
	public static long rangedSum(long n){
		return LongStream.rangeClosed(1,n).reduce(Long::sum).getAsLong();
	}
	
	//并行
	public static long parallelRangedSum(long n){
		return LongStream.rangeClosed(1, n).parallel().reduce(Long :: sum).getAsLong();
	}
	
	public static long sideEffectSum(long n){
		Accumulator accumulator =new Accumulator();
		LongStream.rangeClosed(1, n).parallel().forEach(accumulator::add);
		return accumulator.total;
		
	}
	
	public static long sideEffectParallelSum(long n){
		Accumulator accumulator=new Accumulator();
		LongStream.rangeClosed(1, n).parallel().forEach(accumulator::add);
		return accumulator.total;
	}

	public static class Accumulator{
		private long total =0;
		
		public void add(long value){
			total +=value;
		}
	}
	
}
